<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends controller
{
    public function index()
    {
        return view();
    }
    public function add_order()
    {
        return view();
    }
    public function search()
    {
        return view();
    }

    public function addOrder()
    {
        $input = input();
        $data['name'] = htmlspecialchars($input['name']);
        $data['phone'] = htmlspecialchars($input['phone']);
        $data['type'] = htmlspecialchars($input['type']);
        $data['num'] = htmlspecialchars($input['num']);
        $data['address'] = htmlspecialchars($input['address']);
        $data['create_time'] = time();
        $res = Db::name('order')->insert($data);
        if($res){
            echo json_encode(array('code'=>1,'msg'=>'下单成功'));
        }else{
            echo json_encode(array('code'=>0,'msg'=>'下单失败'));
        }
    }

    public function curl_post($url,$data)
    {
        $data = $data;
        $url = $url;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $rawdata = curl_exec($ch);
        curl_close($ch);
        return $rawdata;
    }
    public function curl_get($url)
    {
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
        $content = curl_exec($ch);
        return $content;
    }

    /**
     * 获取数据
     */
    public function get_data()
    {

        $url = "http://kd.dh.cx/ff4ac";
        $data['query_str'] = trim(input('query_str'));
        $query_str = $data['query_str'];
        $rawdata =  $this->curl_post($url,$data);
        //单个物流
        $data_single = $this->get_single_info($rawdata,$query_str);
        //多个物流
        $data_muti = $this->get_multi_info($rawdata,$query_str);
        if(!isset($data_single['goods_name']) && !$data_muti)
            echo json_encode(array('type'=>0,'data'=>'','msg'=>'没有物流信息'));
        if(!$data_muti && isset($data_single['goods_name'])){
            echo json_encode(array('type'=>1,'data'=>$data_single,'msg'=>'一条物流信息'));
        }else if($data_muti){
            echo json_encode(array('type'=>2,'data'=>$data_muti,'msg'=>'多条物流信息'));
        }



    }

    /**
     * 单个物流
     */
    public function get_single_info($rawdata,$info)
    {
        $data = array();
        $regex_single="/<ul class=\"list-inline\".*?>(.*?)<\/ul>/ism";
        if(preg_match_all($regex_single, $rawdata, $matche_single)){
            if($matche_single){
                $child_reg = "/<li><strong>(.*?)<\/strong>(.*?)<\/li>/ism";
                preg_match_all($child_reg, $matche_single[1][0], $matche_single_child);
                $data['goods_name'] = trim(str_replace('：','',$matche_single_child[2][0]));
                $data['order_no'] = trim(str_replace('：','',$matche_single_child[2][1]));
            }
        }
        return $data;
    }

    /**
     * 多个物流
     */
    public function get_multi_info($rawdata,$info)
    {
        $data = array();
        $regex="/<ul class=\"nav nav-pills\".*?>.*?<\/ul>/ism";
        if(preg_match_all($regex, $rawdata, $matches)){
            if($matches){
                $reg = "/<a href=\"(.*?)\">(.*?)<\/a>/ism";
                preg_match_all($reg, $matches[0][0], $matchess);
                foreach($matchess[2] as $k=>$v){
                    $rawdata_child = $this->curl_get('http://kd.dh.cx/ff4ac/'.urlencode($info).'/'.$k);
                    $data[] = $this->get_single_info($rawdata,$info);
                }
            }
        }
        return $data;
    }
}
